﻿using System.Web;
using ESPNavigatorDefinition=Com.FastSearch.SharePoint.BL.SearchManager.ESPNavigatorDefinition;
using Com.FastSearch.SharePoint.Interfaces;
using Com.FastSearch.SharePoint.WebControls.ESPSearchWebParts.UI;
using ESPWebPartsUnitTest.mock;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ESPWebPartsUnitTest
{
    /// <summary>
    /// Summary description for SearchRequestTest
    /// </summary>
    [TestClass]
    public class ESPNavigatorTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes

        private ESPNavigatorMock navigatorMock;
        private ESPNavigatorItemMock navigatorItemMock;
        private ESPSearchResultMock resultMock;
        private ESPNavigator navigator;

        [TestInitialize]
        public void SetUp()
        {
            navigatorMock = new ESPNavigatorMock();
            navigatorItemMock = new ESPNavigatorItemMock();
            resultMock = new ESPSearchResultMock();
            navigator = new ESPNavigator();
            navigator.Navigator = navigatorMock;
            navigator.SearchResult = resultMock;
        }

        [TestCleanup]
        public void MyTestCleanup()
        {
            navigatorMock = null;
            resultMock = null;
        }

        #endregion

        /// <summary>
        /// Test the GetCount() method
        /// </summary>
        [TestMethod]
        public void GetCountTest()
        {
            GetCountMethod GetCount = delegate(ESPNavigator navigator, IESPNavigatorItem item)
            {
                return navigator.GetCount(item);
            };

            this.navigator.Count = ESPNavigator.CountStrategy.AlwaysCount;
            DoGetCountTest(123, 100, 80, "~80", GetCount);
            DoGetCountTest(123, 123, 80, "80", GetCount);
            DoGetCountTest(12345, 12345, 80, "80", GetCount);

            this.navigator.Count = ESPNavigator.CountStrategy.AlwaysPercentage;
            DoGetCountTest(123, 100, 80, "~80.00%", GetCount);
            DoGetCountTest(123, 123, 80, "65.04%", GetCount);
            DoGetCountTest(12345, 12345, 80, "0.65%", GetCount);

            this.navigator.Count = ESPNavigator.CountStrategy.CountFallbackToNone;
            DoGetCountTest(123, 100, 80, "", GetCount);
            DoGetCountTest(123, 123, 80, "80", GetCount);
            DoGetCountTest(12345, 12345, 80, "80", GetCount);

            this.navigator.Count = ESPNavigator.CountStrategy.CountFallbackToPercentage;
            DoGetCountTest(123, 100, 80, "~80.00%", GetCount);
            DoGetCountTest(123, 123, 80, "80", GetCount);
            DoGetCountTest(12345, 12345, 80, "80", GetCount);

            this.navigator.Count = ESPNavigator.CountStrategy.None;
            DoGetCountTest(123, 100, 80, "", GetCount);
            DoGetCountTest(123, 123, 80, "", GetCount);
            DoGetCountTest(12345, 12345, 80, "", GetCount);
        }

        /// <summary>
        /// Test the GetCountAsNumber() method
        /// </summary>
        [TestMethod]
        public void GetCountAsNumberTest()
        {
            GetCountMethod GetCountAsNumber = delegate(ESPNavigator navigator, IESPNavigatorItem item)
            {
                return navigator.GetCountAsNumber(item);
            };
            DoGetCountTest(123, 100, 80, "~80", GetCountAsNumber);
            DoGetCountTest(123, 123, 80, "80", GetCountAsNumber);
            DoGetCountTest(12345, 12345, 80, "80", GetCountAsNumber);
        }

        /// <summary>
        /// Test the GetCountAsPercentage() method
        /// </summary>
        [TestMethod]
        public void GetCountAsPercentageTest()
        {
            GetCountMethod GetCountAsPercentage = delegate(ESPNavigator navigator, IESPNavigatorItem item)
            {
                return navigator.GetCountAsPercentage(item);
            };
            DoGetCountTest(123, 100, 80, "~80.00%", GetCountAsPercentage);
            DoGetCountTest(123, 123, 80, "65.04%", GetCountAsPercentage);
            DoGetCountTest(12345, 12345, 80, "0.65%", GetCountAsPercentage);
        }

        private void DoGetCountTest(int hits, int hitsUsed, int resultCount, string expected, GetCountMethod getCount)
        {
            resultMock.DocCount = hits;
            navigatorMock.HitsUsed = hitsUsed;
            navigatorItemMock.ResultCount = resultCount;
            Assert.AreEqual(expected, getCount(navigator, navigatorItemMock));
        }

        private delegate string GetCountMethod(ESPNavigator navigator, IESPNavigatorItem item);

        /// <summary>
        /// Test the IsCountExact() method
        /// </summary>
        [TestMethod]
        public void IsCountExactTest()
        {
            navigatorMock.HitsUsed = 100;
            resultMock.DocCount = 123;
            Assert.IsFalse(navigator.IsCountExact());

            navigatorMock.HitsUsed = 123;
            resultMock.DocCount = 123;
            Assert.IsTrue(navigator.IsCountExact());
        }


        /// <summary>
        /// Test the IsScopeNavigator() method
        /// </summary>
        [TestMethod]
        public void IsScopeNavigatorTest()
        {
            navigator.NavigatorDefinition = new ESPNavigatorDefinition("foo", "Foo", false);
            Assert.IsFalse(navigator.IsScopeNavigator());

            navigator.NavigatorDefinition = new ESPNavigatorDefinition("bar", "Bar", true);
            Assert.IsTrue(navigator.IsScopeNavigator());
        }
    }
}